<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/14
 * Time: 11:08
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class OrderController extends BaseController
{
    protected $service;

    public function __construct(Request $request, OrderService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 结算页下单
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function prepareOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'settle_list'   =>  'required|array',
            'settle_list.*.good_id' =>  'required|integer|exists:mini_product,id',
            'settle_list.*.spec_id' =>  'integer|exists:mini_product_specs,id',
            'settle_list.*.good_number' =>  'required|integer',
            'coupon_id'     =>  'integer|exists:mini_user_coupon,coupon_id',
            'address_id'    =>  'required|integer|exists:mini_user_address,id',
            'remark'        =>  'string|max:1000'
        ], [
            'settle_list.required'    =>  '商品列表不能为空',
            'address_id.required'     =>  '用户地址不能为空',
            'settle_list.array'       =>  '商品列表必须为数组',

            'settle_list.*.good_id.required'    =>  '商品ID不能为空',
            'settle_list.*.spec_id.required'    =>  '规格ID不能为空',
            'settle_list.*.good_number.required'=>  '商品数量不能为空',

            'settle_list.*.good_id.integer'    =>  '商品ID必须为整数',
            'settle_list.*.spec_id.integer'    =>  '规格ID必须为整数',
            'settle_list.*.good_number.integer'=>  '商品数量必须为整数',
            'address_id.integer'    =>  '用户地址必须为整数',

            'settle_list.*.good_id.exists'     =>  '商品不存在',
            'settle_list.*.spec_id.exists'     =>  '规格不存在',
            'address_id.exists'     =>  '用户地址不存在',

            'coupon_id.integer'    =>  '优惠券ID必须为整数',
            'coupon_id.exists'     =>  '优惠券不存在',

            'remark.string'   =>    '用户备注必须为字符串',
            'remark.max'      =>    '用户地址备注过长'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }

        $good_array     =   $this->request->post('settle_list', []);
        if (empty($good_array)) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }
        $good_array     =   $this->goodRegroup($good_array);
        $address_id     =   $this->request->post('address_id', NULL);
        $coupon_id      =   $this->request->post('coupon_id', NULL);
        $remark         =   $this->request->post('remark', '');
        $is_form_cart   =   $this->request->post('is_from_cart', 0);
        $invite_unique  =   $this->request->post('invite_unique', NULL);
        $live_unique    =   $this->request->post('live_unique', NULL);
        $box_unique     =   $this->request->post('box_unique', NULL);
        $order_extend   =   [
            'invite_unique' =>  $invite_unique,
            'live_unique'   =>  $live_unique,
            'box_unique'    =>  $box_unique,
        ];

        [$flag, $result]=   $this->service->orderPrepare($good_array, $address_id, $coupon_id, $remark, (int) $is_form_cart, $order_extend);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ORDER_PREPARE_ERROR, $result);
        }
    }

    /**
     * 结算页商品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function settleOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'settle_list'   =>  'required|array',
            'settle_list.*.good_id' =>  'required|integer|exists:mini_product,id',
            'settle_list.*.spec_id' =>  'integer|exists:mini_product_specs,id',
            'settle_list.*.good_number' =>  'required|integer',
//            'address_id'    =>  'integer|exists:mini_user_address,id',
        ], [
            'settle_list.required'    =>  '商品列表不能为空',
            'settle_list.array'       =>  '商品列表必须为数组',

            'settle_list.*.good_id.required'    =>  '商品ID不能为空',
            'settle_list.*.spec_id.required'    =>  '规格ID不能为空',
            'settle_list.*.good_number.required'=>  '商品数量不能为空',

            'settle_list.*.good_id.integer'    =>  '商品ID必须为整数',
            'settle_list.*.spec_id.integer'    =>  '规格ID必须为整数',
            'settle_list.*.good_number.integer'=>  '商品数量必须为整数',

            'settle_list.*.good_id.exists'     =>  '商品不存在',
            'settle_list.*.spec_id.exists'     =>  '规格不存在',

            'address_id.integer'    =>  '用户地址ID必须为整数',
            'address_id.exists'     =>  '用户地址不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }

        $good_array     =   $this->request->post('settle_list', []);
        $address_id     =   $this->request->post('address_id', NULL);
        if (empty($good_array)) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }
        $good_array     =   $this->goodRegroup($good_array);
        [$flag, $result]    =   $this->service->orderSettle($good_array, $address_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $result);
        }
    }

    /**
     * 商品更改计算总价格
     * @return \Illuminate\Http\JsonResponse
     */
    public function calcOrder()
    {
        $validator  =   Validator::make($this->request->post(), [
            'settle_list'   =>  'required|array',
            'settle_list.*.good_id' =>  'required|integer|exists:mini_product,id',
            'settle_list.*.spec_id' =>  'integer|exists:mini_product_specs,id',
            'settle_list.*.good_number' =>  'required|integer',
            'coupon_id'     =>  'integer|exists:mini_user_coupon,coupon_id'
        ], [
            'settle_list.required'    =>  '商品列表不能为空',
            'settle_list.array'       =>  '商品列表必须为数组',

            'settle_list.*.good_id.required'    =>  '商品ID不能为空',
            'settle_list.*.spec_id.required'    =>  '规格ID不能为空',
            'settle_list.*.good_number.required'=>  '商品数量不能为空',

            'settle_list.*.good_id.integer'    =>  '商品ID必须为整数',
            'settle_list.*.spec_id.integer'    =>  '规格ID必须为整数',
            'settle_list.*.good_number.integer'=>  '商品数量必须为整数',

            'settle_list.*.good_id.exists'     =>  '商品不存在',
            'settle_list.*.spec_id.exists'     =>  '规格不存在',

            'coupon_id.integer'    =>  '优惠券ID必须为整数',
            'coupon_id.exists'     =>  '优惠券不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }

        $good_array     =   $this->request->post('settle_list', []);
        if (empty($good_array)) {
            return $this->errorResponse(ErrorConstant::API_ORDER_SETTLE_ERROR, $validator->errors()->first());
        }
        $good_array     =   $this->goodRegroup($good_array);
        $coupon_id      =   $this->request->post('coupon_id', NULL);
        $total_result   =   $this->service->orderCalc($good_array, $coupon_id);
        unset($total_result['total_good']);
        return $this->successResponse($total_result);
    }

    /**
     * 数组重组
     * @param array $good_array
     * @return array
     */
    protected function goodRegroup(array $good_array)
    {
        foreach ($good_array as &$item) {
            $item['spec_id']    =   $item['spec_id'] ?? 0;
        }
        return $good_array;
    }

}
